package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.*;
import com.zhongyouyun.admin.pojo.UserDetailPojo;
import com.zhongyouyun.admin.pojo.UserPojo;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.UserService;
import sun.security.provider.MD5;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 用户表
 */
@RestController
@RequestMapping("/admin/z_user")
@Api(value = "用户信息controller", tags = {"用户信息操作接口"})
public class UserController {
    @Autowired
    private UserService service;

    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "获取用户信息", notes = "通过用户姓名获取用户信息")
    public IPage<UserPojo> findPage(@RequestBody PageRequest pageRequest) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
        return service.selectPojoPage(page, queryWrapper);
    }

    @PostMapping("/findPermissions")
    @ResponseBody
    @ApiOperation(value = "获取用户信息", notes = "通过用户姓名获取用户信息")
    public Result findPermissions(@RequestBody UserModel userModel) {
        List<String> list = service.findPermissions(userModel);
        if (list.size() > 0) {
            Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
            resultMap.put("list", list);
            return Result.builder().message("成功").status(0).data(resultMap).build();
        } else {
            return Result.builder().message("没有任何权限").status(1).build();
        }
    }

    @PostMapping("/getDetails")
    @ResponseBody
    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    public Result getDetails(@ApiParam(name = "id") Long id) {
        UserDetailPojo userDetailPojo = service.getDetails(id);

        if (ObjectUtil.isNotEmpty(userDetailPojo)) {
            return Result.builder().message("成功").status(0).data(userDetailPojo).build();
        } else {
            return Result.builder().message("失败").status(1).build();
        }
    }

    @PostMapping("/upState")
    @ResponseBody
    @ApiOperation(value = "修改用户停用状态", notes = "通过用户ID修改用户状态")
    public Result upState(@ApiParam(name = "userId") Long userId, @ApiParam(name = "state", value = "0停用 1启动") String state) {

        UserModel userModel = service.getById(userId);
        userModel.setState(state);
        Boolean flag = service.updateById(userModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("失败").status(1).build();
        }


    }


    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody UserModel userModel) {
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(model.getId()) && userModel.getId() != 0) {
            userModel.setUpdateBy(model.getId().toString());
            UserModel u = service.getById(model.getId());
            userModel.setPassword(u.getPassword());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(userModel, model.getId().toString());
            userModel.setPassword(SecureUtil.md5(userModel.getPassword()));
        }
        Boolean flag = service.saveUser(userModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<UserModel> userModelList) {

        List<Long> list = new ArrayList<Long>();
        userModelList.forEach(userModel -> {
            list.add(userModel.getId());
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

}
